package com.wty.cmims.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wty.cmims.pojo.Code;
import com.wty.cmims.pojo.Resident;
import com.wty.cmims.pojo.ResponseResult;
import com.wty.cmims.service.ResidentService;
import com.wty.cmims.mapper.ResidentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;

/**
* @author wty
* @description 针对表【resident】的数据库操作Service实现
* @createDate 2024-10-04 10:06:36
*/
@Service
public class ResidentServiceImpl extends ServiceImpl<ResidentMapper, Resident>
    implements ResidentService{

    @Autowired
    private ResidentMapper residentMapper;
//    /**
//     * 添加居民信息
//     * @param userId
//     * @return
//     */
//    @Override
//    public ResponseResult<Void> residentInfoAdd(Integer userId) {
//        ResponseResult<Void> responseResult = new ResponseResult<>();
//        // 创建Resident对象赋值并插入数据表
//        Resident resident = new Resident();
//        resident.setUserId(userId);
//        boolean save = this.save(resident);
//        if (save) {
//            responseResult.setCode(Code.SUCCESS);
//            responseResult.setMessage("新增居民信息成功");
//            return responseResult;
//        } else {
//            responseResult.setCode(Code.ERROR);
//            responseResult.setMessage("新增居民信息失败");
//        }
//        return responseResult;
//    }

    @Override
    public ResponseResult<Map<String,Object>> residentInfoFind(String account) {
        ResponseResult<Map<String,Object>> responseResult = new ResponseResult<>();
        Map<String,Object> paramsMap = new HashMap<>();
        paramsMap.put("account",account);
        Map<String, Object> map = residentMapper.findResidentInfo(paramsMap);
        if (map != null) {
            responseResult.setCode(Code.SUCCESS);
            responseResult.setMessage("查询居民信息成功");
            responseResult.setData(map);
        } else {
            responseResult.setCode(Code.ERROR);
            responseResult.setMessage("查询居民信息失败");
        }
        return responseResult;
    }

    @Override
    @Transactional
    public ResponseResult<Map<String,Object>> residentInfoUpdate(Map<String,Object> paramsMap) {
        ResponseResult<Map<String,Object>> responseResult = new ResponseResult<>();
        Integer rows = residentMapper.updateResidentInfo(paramsMap);
        if (rows == 2) {
            responseResult.setCode(Code.SUCCESS);
            responseResult.setMessage("居民信息更新成功");
            responseResult.setData(paramsMap);
        } else {
            responseResult.setCode(Code.ERROR);
            responseResult.setMessage("居民信息更新失败");
        }
        return responseResult;
    }
}




